﻿namespace Alkitab
{
    using Alkitab.Language;
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Shell;
    using System;
    using System.Collections.Generic;
    using System.Windows;
    using System.Windows.Controls;

    public partial class Pilihan : PhoneApplicationPage
    {
        #region Fields

        public static bool needRefresh = false;
        private List<string> daftarAyat = new List<string>();
        private bool terjemahanlamakah;

        public bool Terjemahanlamakah
        {
            get { return terjemahanlamakah; }
            set
            {
                Globals.terjemahanlamakah = value;
                terjemahanlamakah = value;
            }
        }

        #endregion Fields

        #region Constructors

        public Pilihan()
        {
            InitializeComponent();
            BuildApplicationBar();
            Terjemahanlamakah = true;
            Globals.nobuku = 0;
            Globals.nopasal = 0;

            Globals.versiKitab = (string)Globals.Settingan["versiKitab"];

            oldBukuList.ItemsSource = Globals.daftarBukuA;
            newBukuList.ItemsSource = Globals.daftarBukuN;

            if (!Globals.Settingan.Contains("infoPilihanDisplayed?"))
            {
                MessageBoxResult result = MessageBox.Show(Labels.informationPilihan, Labels.information, MessageBoxButton.OK);
                if (result == MessageBoxResult.OK)
                {
                    Globals.Settingan.Add("infoPilihanDisplayed?", 1);
                }
            }
        }

        #endregion Constructors

        #region Methods

        public void refreshPage()
        {
            oldBukuList.ItemsSource = null;
            newBukuList.ItemsSource = null;
            oldPasalList.ItemsSource = null;
            newPasalList.ItemsSource = null;
            Globals.nobuku = 0;
            Globals.nopasal = 0;
            oldBukuList.ItemsSource = Globals.daftarBukuA;
            newBukuList.ItemsSource = Globals.daftarBukuN;
            BuildApplicationBar();
            needRefresh = false;
        }

        private void BuildApplicationBar()
        {
            ApplicationBar = new ApplicationBar();

            ApplicationBarIconButton appBarButton2 = new ApplicationBarIconButton(new Uri("Images/ApplicationBar/h.png", UriKind.Relative));
            appBarButton2.Text = Labels.history;
            appBarButton2.Click += new EventHandler(history_Click);
            ApplicationBar.Buttons.Add(appBarButton2);

            ApplicationBarMenuItem searchTextMenuItem = new ApplicationBarMenuItem(Labels.searchText);
            searchTextMenuItem.Click += new EventHandler(search_Click);
            ApplicationBar.MenuItems.Add(searchTextMenuItem);
            ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(Labels.chlanguage);
            appBarMenuItem.Click += new EventHandler(gantiB_Click);
            ApplicationBar.MenuItems.Add(appBarMenuItem);
        }

        private void gantiB_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/View/Options.xaml?Page=1", UriKind.Relative));
        }

        private void history_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/View/HistoryAndBookmark.xaml", UriKind.Relative));
        }

        private void newBuku_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Globals.nobuku = 0;
            if (!(newBukuList.SelectedIndex == -1)) Globals.nobuku = newBukuList.SelectedIndex + 1;

            Globals.nopasal = 0;
            newPasalList.ItemsSource = null;

            switch (Globals.nobuku)
            {
                case 1: setJumlahPasalNew(28); break;
                case 2: setJumlahPasalNew(16); break;
                case 3: setJumlahPasalNew(24); break;
                case 4: setJumlahPasalNew(21); break;
                case 5: setJumlahPasalNew(28); break;
                case 6: setJumlahPasalNew(16); break;
                case 7: setJumlahPasalNew(16); break;
                case 8: setJumlahPasalNew(13); break;
                case 9: setJumlahPasalNew(6); break;
                case 10: setJumlahPasalNew(6); break;
                case 11: setJumlahPasalNew(4); break;
                case 12: setJumlahPasalNew(4); break;
                case 13: setJumlahPasalNew(5); break;
                case 14: setJumlahPasalNew(3); break;
                case 15: setJumlahPasalNew(6); break;
                case 16: setJumlahPasalNew(4); break;
                case 17: setJumlahPasalNew(3); break;
                case 18: setJumlahPasalNew(1); break;
                case 19: setJumlahPasalNew(13); break;
                case 20: setJumlahPasalNew(5); break;
                case 21: setJumlahPasalNew(5); break;
                case 22: setJumlahPasalNew(3); break;
                case 23: setJumlahPasalNew(5); break;
                case 24: setJumlahPasalNew(1); break;
                case 25: setJumlahPasalNew(1); break;
                case 26: setJumlahPasalNew(1); break;
                case 27: setJumlahPasalNew(22); break;
                default: setJumlahPasalNew(0); break;
            }
            newPasalList.ItemsSource = Globals.daftarPasalNew;
        }

        private void newPasal_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!(newPasalList.SelectedIndex == -1))
            {
                Globals.nobuku = newBukuList.SelectedIndex + 1;
                Globals.nopasal = newPasalList.SelectedIndex + 1;

                Globals.terjemahansblmnya = Terjemahanlamakah;
                Globals.nopasalsblmnya = Globals.nopasal;
                Globals.nobukusblmnya = Globals.nobuku;
                Globals.noayat = 0;

                Globals.stopupdatinghistory = false;
                Globals.dunnawhhattonameit = false;
                NavigationService.Navigate(new Uri("/View/Bacaan.xaml", UriKind.Relative));
                newPasalList.SelectedIndex = -1;
            }
        }

        private void oldBuku_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Globals.nobuku = 0;
            if (!(oldBukuList.SelectedIndex == -1)) Globals.nobuku = oldBukuList.SelectedIndex + 1;

            Globals.nopasal = 0;
            oldPasalList.ItemsSource = null;

            switch (Globals.nobuku)
            {
                case 1: setJumlahPasalOld(50); break;
                case 2: setJumlahPasalOld(40); break;
                case 3: setJumlahPasalOld(27); break;
                case 4: setJumlahPasalOld(36); break;
                case 5: setJumlahPasalOld(34); break;
                case 6: setJumlahPasalOld(24); break;
                case 7: setJumlahPasalOld(21); break;
                case 8: setJumlahPasalOld(4); break;
                case 9: setJumlahPasalOld(31); break;
                case 10: setJumlahPasalOld(24); break;
                case 11: setJumlahPasalOld(22); break;
                case 12: setJumlahPasalOld(25); break;
                case 13: setJumlahPasalOld(29); break;
                case 14: setJumlahPasalOld(36); break;
                case 15: setJumlahPasalOld(10); break;
                case 16: setJumlahPasalOld(13); break;
                case 17: setJumlahPasalOld(10); break;
                case 18: setJumlahPasalOld(42); break;
                case 19: setJumlahPasalOld(150); break;
                case 20: setJumlahPasalOld(31); break;
                case 21: setJumlahPasalOld(12); break;
                case 22: setJumlahPasalOld(8); break;
                case 23: setJumlahPasalOld(66); break;
                case 24: setJumlahPasalOld(52); break;
                case 25: setJumlahPasalOld(5); break;
                case 26: setJumlahPasalOld(48); break;
                case 27: setJumlahPasalOld(12); break;
                case 28: setJumlahPasalOld(14); break;
                case 29: setJumlahPasalOld(3); break;
                case 30: setJumlahPasalOld(9); break;
                case 31: setJumlahPasalOld(1); break;
                case 32: setJumlahPasalOld(4); break;
                case 33: setJumlahPasalOld(7); break;
                case 34: setJumlahPasalOld(3); break;
                case 35: setJumlahPasalOld(3); break;
                case 36: setJumlahPasalOld(3); break;
                case 37: setJumlahPasalOld(2); break;
                case 38: setJumlahPasalOld(14); break;
                case 39: setJumlahPasalOld(4); break;
                default: setJumlahPasalOld(0); break;
            }
            oldPasalList.ItemsSource = Globals.daftarPasalOld;
        }

        private void oldPasal_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!(oldPasalList.SelectedIndex == -1))
            {
                Globals.nobuku = oldBukuList.SelectedIndex + 1;
                Globals.nopasal = oldPasalList.SelectedIndex + 1;

                Globals.terjemahansblmnya = Terjemahanlamakah;
                Globals.nopasalsblmnya = Globals.nopasal;
                Globals.nobukusblmnya = Globals.nobuku;
                Globals.noayat = 0;

                Globals.stopupdatinghistory = false;
                Globals.dunnawhhattonameit = false;
                NavigationService.Navigate(new Uri("/View/Bacaan.xaml", UriKind.Relative));
                oldPasalList.SelectedIndex = -1;
            }
        }

        private void page_Loaded(object sender, RoutedEventArgs e)
        {
            if (needRefresh) refreshPage();
        }

        private void Pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (pivot.SelectedIndex == 1)
            {
                Globals.nobuku = 0;
                Globals.nopasal = 0;
                Terjemahanlamakah = false;
            }
            else
            {
                Globals.nobuku = 0;
                Globals.nopasal = 0;
                Terjemahanlamakah = true;
            }
        }

        private void search_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/View/SearchVerse.xaml", UriKind.Relative));
        }
        private void setJumlahPasalNew(int angka)
        {
            if (Globals.daftarPasalNew.Count > 0)
                Globals.daftarPasalNew.Clear();
            if (angka > 0)
            {
                for (int i = 1; i <= angka; i++)
                {
                    Globals.daftarPasalNew.Add(Convert.ToString(i));
                }
            }
        }

        private void setJumlahPasalOld(int angka)
        {
            if (Globals.daftarPasalOld.Count > 0)
                Globals.daftarPasalOld.Clear();
            if (angka > 0)
            {
                for (int i = 1; i <= angka; i++)
                {
                    Globals.daftarPasalOld.Add(Convert.ToString(i));
                }
            }
        }
        #endregion Methods
    }
}